
import java.util.Random;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class LoadGameScreen extends GameCanvas implements Runnable {

    private int delay = 250;
    //   private Image backGround;
    private Graphics g;
    private boolean isActive;
    private Main iMain;
    private MenuScreen iMenuScreen;
    private int time = 0;
    private int dem = 0;
    

    public LoadGameScreen(Main aMain) {
        super(true);
        setFullScreenMode(true);
        iMain = aMain;
    }

    public void Start() {

        isActive = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        draw();
        while (isActive) {

            update();
            drawNext();            

            if (time == 200) {
                iMenuScreen = new MenuScreen(iMain);
                iMenuScreen.Start();
                Display.getDisplay(iMain).setCurrent(iMenuScreen);
                stop();
            }

            flushGraphics();
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }
    Random rd = new Random();

    public void update() {
        int n = rd.nextInt(25);
        time = time + n;
        
        dem ++;
        if (time > 200) {
            time = 200;
        }
    }

    public void draw() {
        this.g = this.getGraphics();

        g.setColor(207, 207, 207);
        g.fillRect(0, 0, 240, 320);
        
        g.setColor(0,0,0);
        g.drawRect(19, 139, 200, 8);
        g.drawRect(18, 138, 200, 10);

        this.flushGraphics();
    }
    
    public void drawNext() {
        this.g = this.getGraphics();

        g.setColor(139, 35 ,35);
        g.fillRect(20, 140, time, 7);
        
        // Loading...
        g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.FONT_STATIC_TEXT, Font.SIZE_LARGE));
        if(dem%8 == 2)
        {
            g.setColor(207, 207, 207);
            g.fillRect(138, 150, 50, 50);
            g.setColor(0, 0, 0);
            g.drawString("Loading", 80, 155, Graphics.TOP | Graphics.LEFT);
        }
        else if(dem%8 == 4)
        {
            g.setColor(207, 207, 207);
            g.fillRect(143, 150, 50, 50);
            g.setColor(0, 0, 0);
            g.drawString("Loading.", 80, 155, Graphics.TOP | Graphics.LEFT);
        }
        else if(dem%8 == 6)
        {
            g.setColor(207, 207, 207);
            g.fillRect(145, 150, 50, 50);
            g.setColor(0, 0, 0);
            g.drawString("Loading..", 80, 155, Graphics.TOP | Graphics.LEFT);
        }
        else if(dem%8 == 0 )
        {
            g.setColor(0, 0, 0);
            g.drawString("Loading...", 80, 155, Graphics.TOP | Graphics.LEFT);
        }
        this.flushGraphics();
    }

    public void stop() {
        isActive = false;
    }
}
